//
//  MLTabBar.m
//  Molian
//
//  Created by 胡津 on 15/3/16.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//
#define BadgeTag 100000

#import "MLTabBar.h"
//#import "ChatListViewController.h"
#import "MeViewController.h"//我
//#import "MLHomeViewController.h"
//#import "FindViewController.h"
#import "FirstViewController.h"
#import "LoginViewController.h"
#import "OonlineViewController.h"//发现
#import "RecommendViewController.h"//专属恋人
#import "ApplePayViewController.h"

@interface MLTabBar ()<UITabBarControllerDelegate>

@end

@implementation MLTabBar


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    for (int i = 0 ; i < 5; i++) {
        //角标
        UIView *Badge = [[UIView alloc]initWithFrame:CGRectMake(((DeviceWidth/5)*i)+((DeviceWidth/5)/2)+10, 10, 7.5, 7.5)];
        Badge.layer.masksToBounds = YES;//开启圆角
        [Badge.layer setCornerRadius:7.5/2];
        Badge.backgroundColor = UIColorFromRGB(0xFE6A66);
        Badge.tag = i + BadgeTag;
        Badge.alpha = 0;
        [self.tabBar addSubview:Badge];
    }
    
    //恢复定制恋人电话
    [Utils saveToDefaults:@"NO" forKey:@"callCustomizeLoverPhone"];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6)
    {
        [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    }
    
    NSMutableArray *viewControllers = [NSMutableArray arrayWithObjects:[[FirstViewController alloc] init],[[OonlineViewController alloc] init],[[RecommendViewController alloc] init],[[MeViewController alloc] init],nil];
    NSArray *viewControllerTitleArr = [NSArray arrayWithObjects:@"试恋",@"会员",@"日志",@"我", nil];
    NSArray *unSelectedImageArr= @[@"love_normal_iconA",@"vip_normal_icon",@"daily_normal_icon",@"me_normal_icon"];
    NSArray *selectedImageArr = @[@"love_press_icon",@"vipe_press_icon",@"dailye_press_icon",@"me_press_icon"];
    NSMutableArray *mutableArr = [NSMutableArray array];
   // [ShareData shareData].chatlistVC = viewControllers[2];
    for (int i = 0; i < viewControllers.count; i++)
    {
        UINavigationController *navigation;
        navigation = [[UINavigationController alloc] initWithRootViewController:[viewControllers objectAtIndex:i]];
        navigation.tabBarItem = [[UITabBarItem alloc] initWithTitle:[viewControllerTitleArr objectAtIndex:i] image:[[UIImage imageNamed:[unSelectedImageArr objectAtIndex:i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:[selectedImageArr objectAtIndex:i]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        //非选中时的样式
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:FontName size:11.0f],NSFontAttributeName,[UIColor grayColor],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
        //选中时的样式
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:FontName size:11.0f],NSFontAttributeName,APPCOLOR(1),NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
        
        [mutableArr addObject:navigation];
        if (i == viewControllers.count) {
            navigation.tabBarItem.badgeValue = @"";
        }
        
        switch (i) {
            case 0:
                self.firstNavigation = navigation;
                break;
            case 1:
                self.homeNavigation = navigation;
                break;
//            case 2:
//                self.msgNavigation = navigation;
//                break;
            case 2:
                self.findNavigation = navigation;
                break;
            
            case 3:
                self.meNavigation = navigation;
                break;
            default:
                break;
        }
    }
    self.viewControllers = mutableArr;
    self.tabBarController.tabBar.barStyle = UIBarStyleDefault;
    //    self.tabBar.tintColor = APPCOLOR(1);
    self.selectedIndex = 0;
    self.delegate = self;
    
   // [self unreadMsgCount];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cmd:) name:@"cmd" object:nil];
  //  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(message) name:@"message" object:nil];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loveMessage) name:@"loveMessage" object:nil];
    
    
    
   
    
    

}
- (void)dealloc
{
     [[NSNotificationCenter defaultCenter] removeObserver:self];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"cmd" object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"message" object:nil];
}

//-(void)unreadMsgCount
//{
//    NSInteger unreadMsgCount = [[Utils defaultsForKey:@"unreadMsg"] integerValue];
//    
//    if (unreadMsgCount == 0) {
//        _msgNavigation.tabBarItem.badgeValue = nil;
//    }else{
////        _msgNavigation.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",(long)unreadMsgCount++];
//        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//        [defaults setInteger:unreadMsgCount forKey:@"unreadMsg"];
//        [defaults synchronize];
//    }
//}

//-(void)cmd:(NSNotification *)notification
//{
//    NSDictionary *dic=[notification userInfo];
//    EMMessage *message = [dic objectForKey:@"message"];
////    NSLog(@"=========%@",message);
//    EMCommandMessageBody *body= message.messageBodies.lastObject;
//
//    
//    if (message.from&&[message.from isEqualToString:@"new_friend"]) {
//        EMCommandMessageBody *body= message.messageBodies.lastObject;
//        if (body.action && [body.action isEqualToString:ACTION_FRIEND_APPLY]) {
//            UIView *Badge = (UIView*)[self.tabBar viewWithTag:BadgeTag+2];
//            [Utils Hidden:Badge Bool:YES];
//            [Utils saveToDefaults:@"YES" forKey:@"new_friend"];
//        }
//    }else if ([[[notification userInfo] objectForKey:@"type"] isEqualToString:@"New_msg"]){
//        UIView *Badge = (UIView*)[self.tabBar viewWithTag:BadgeTag+2];
//        [Utils Hidden:Badge Bool:YES];
//        [self unreadMsgCount];
//    }else if ([message.from isEqualToString:@"new_liker"]){
//        UIView *Badge = (UIView*)[self.tabBar viewWithTag:BadgeTag+4];
//        [Utils Hidden:Badge Bool:YES];
//        [Utils saveToDefaults:@"YES" forKey:@"new_liker"];
//    }
//}
//- (void)loveMessage
//{
//    UIView *Badge = (UIView*)[self.tabBar viewWithTag:BadgeTag];
//    [Utils Hidden:Badge Bool:YES];
//    [self unreadMsgCount];
//}
//-(void)message
//{
//    UIView *Badge = (UIView*)[self.tabBar viewWithTag:BadgeTag+2];
//    [Utils Hidden:Badge Bool:YES];
//    [self unreadMsgCount];
//}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController*)viewController
{
    UIView *Badge = (UIView*)[self.tabBar viewWithTag:BadgeTag+tabBarController.selectedIndex];
    if (Badge.alpha == 1) {
        [Utils Hidden:Badge Bool:NO];
    }
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController*)viewController
{
    if (viewController == [tabBarController.viewControllers objectAtIndex:1] || viewController == [tabBarController.viewControllers objectAtIndex:2] || viewController == [tabBarController.viewControllers objectAtIndex:3]) {
        MLUser *currentUser = [MLUser currentUser];
        if (currentUser && currentUser.isUserInited) {
            return YES;
        }else{
            
            if (currentUser != nil) {
                if (viewController == [tabBarController.viewControllers objectAtIndex:3]) {
                    return YES;
                }
                
                
             
                if (!currentUser.isUserInited) {
                   
                    if ([Utils cheackConfigurationScreen]) {
                        NSString *str = [Utils defaultsForKey:IAP_ID_98_YUAN];
                        if (str&&[str isEqualToString:@"YES"]) {
                            return YES;
                        }else{
                            ApplePayViewController *applyPVC = [[ApplePayViewController alloc] init];
                            
                            applyPVC.hidesBottomBarWhenPushed = YES;
                            [[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex] pushViewController:applyPVC animated:YES];
                            return NO;
                        }
                    }else{
                        return YES;
                    }
                    
                    
                    
                    
                    
                   
                }else{
                    return YES;
                }
                
                
            }else{
                
                if (viewController == [tabBarController.viewControllers objectAtIndex:2]) {
                    return YES;
                }
                // 如果用户已填写了初始化信息
//                UINavigationController *Login = [[UINavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
//                Login.modalPresentationStyle = UIModalPresentationFormSheet;
//                
//                [self presentViewController:Login animated:YES completion:nil];
//                NSLog(@"%@",viewController);
               
                NSLog(@"%ld",tabBarController.selectedIndex);
                
                
                LoginViewController *login = [[LoginViewController alloc]init];
            //    login.modalPresentationStyle = UIModalPresentationFormSheet;
                login.hidesBottomBarWhenPushed = YES;
                [[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex] pushViewController:login animated:YES];
                return NO;
            }
            
            
            
        }
    }else{
        return YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}



@end
